<template>
  <div id="app" style="z-index: 10;">
    <img src="../../../../public/images/index/dongman04.jpg" style="width: 1200px;position: relative;top: 4px">
    <!--1578 1987-->
    <el-col :span="8" style="margin: 0 auto;">
      <!--卡片功能-->
      <el-card shadow="hover"
               style="width: 604px;height: 493px;position: fixed;top: 50%;left: 50%;margin-left: -412px;margin-top: -211px;border-radius: 20px">
        <!--用户名和密码登录注册功能-->
        <div>
          <el-form :model="ruleForm" :rules="rules" label-width="60px" ref="ruleForm" class="demo-ruleFrom" style="width: 400px;margin: 50px auto">
            <h2 style="text-align: center">用户注册</h2><br>
            <el-form-item label="*用户名" prop="username">
              <el-input type="text" placeholder="请输入邮箱或手机号" v-model="ruleForm.username"></el-input>
            </el-form-item><br>
            <el-form-item label="*密码" prop="password">
              <el-input type="password" placeholder="请输入密码" v-model="ruleForm.password"></el-input>
            </el-form-item><br>
            <el-form-item label="*手机" prop="telephone">
              <el-input type="text" placeholder="请输入手机号" v-model="ruleForm.telephone"></el-input>
            </el-form-item><br>
            <el-form-item>
              <el-button style="border-radius: 12px; position: relative;right: -100px;" type="primary" @click="submitForm('ruleForm')">注册</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </el-col>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: 'root12522',

        password: 'root112523',
        telephone: '12345352410'
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        telephone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {min: 11, max: 11, message: '长度在 11 个字符', trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:17701/user/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          let localJwt = JSON.parse(localStorage.getItem('localJwt'));

          this.axios
              .create({
                'headers': {
                  'Authorization': localJwt.token
                }
              })
              .post(url, formData).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$message({
                message: '添加用户成功！',
                type: 'success'
              });
              let jwt = jsonResult.data;
              let jwtJsonString = JSON.stringify(jwt)
              console.log(typeof jwt);
              console.log('jwtJsonString: ' + typeof jwtJsonString);
              localStorage.setItem('localJwt', jwtJsonString);
              console.log('已经将登录信息存入到localStorage中，即将跳转页面……');
              this.$router.push('/sys-user/product/login');
            } else {
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  mounted() {
  }
}
</script>

<style>
</style>